package com.gegong.imr.client.utils.http;

import com.alibaba.fastjson.JSON;
import com.gegong.imr.client.tasks.CenterResponse;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @deprecated 使用 <a href="https://github.com/square/okhttp">OkHttp</a>
 */
public class HttpRequestUtils {


    /**
     * 使用post方式发送一个表单并返回json数据
     *
     * @param url
     * @param params
     * @return
     * @deprecated 使用 <a href="https://github.com/square/okhttp">OkHttp</a>
     */
    public static CenterResponse postFormWithJsonRes(String url, Map params) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        HttpEntity entity = new UrlEncodedFormEntity(getPairs(params), CharEncoding.UTF_8);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = httpclient.execute(httpPost);
        String responseStr = getEntityContentString(response.getEntity());
        closeQuietly(httpclient, response);
        return JSON.parseObject(responseStr, CenterResponse.class);
    }


    /**
     * 使用post方式http请求,参数以json格式放在body中
     *
     * @param url
     * @param jsonString
     * @return
     * @deprecated 使用 <a href="https://github.com/square/okhttp">OkHttp</a>
     */
    public static CenterResponse postJsonWithJsonRes(String url, String jsonString) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        StringEntity stringEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response = httpclient.execute(httpPost);
        String responseStr = getEntityContentString(response.getEntity());
        closeQuietly(httpclient, response);
        return JSON.parseObject(responseStr, CenterResponse.class);
    }



    private static void closeQuietly(HttpClient client, CloseableHttpResponse response) {
        HttpClientUtils.closeQuietly(response);
        HttpClientUtils.closeQuietly(client);
    }

    private static List<NameValuePair> getPairs(Map params) {
        List<NameValuePair> pairs = new ArrayList<>();
        if (Objects.nonNull(params) && !params.isEmpty())
            params.forEach((k, v) -> {
                pairs.add(new BasicNameValuePair(k.toString(), v.toString()));
            });

        return pairs;
    }

    private static String getEntityContentString(HttpEntity entity) throws IOException {
        List<String> lines = IOUtils.readLines(entity.getContent());
        StringBuilder content = new StringBuilder();
        lines.forEach(content::append);
        return content.toString();
    }
}
